package com.tj.bo.trade.server.service.rule;

import com.fqgj.common.utils.CollectionUtils;
import com.fqgj.common.utils.DateUtil;
import com.tj.bo.trade.client.enums.OrderStatusEnum;
import com.tj.bo.trade.client.enums.TradeTypeEnum;
import com.tj.bo.trade.server.entity.BinaryTradeEntity;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

/**
 * 新客规则
 * Author: Amarendar
 * Date: 2020/8/25 上午11:01
 */
@Service
public class TradeNewUserRule extends BaseTradeRule {

    private static final String RULE_NAME = "新用户前10笔交易,收益率保持不变";

    private static final Integer DEFAULT_PRIORITY = 2;

    @Override
    public void checkRouterRule(RouterContext routerContext, List<RuleResultVO> ruleResultVOList) {
        Date startDate = DateUtil.getPatternDate(new Date(), "yyyy-MM-dd");
        Date endDate = DateUtil.addDate(startDate, 1);
        boolean isNewUser = binaryTradeDao.isProductNewUser(routerContext.getUserCode(), routerContext.getProductCode());
        if (!isNewUser) {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), RULE_NAME).setMessage("非新客"));
            return;
        }

        List<BinaryTradeEntity> binaryTradeEntityList = binaryTradeDao.selectsBinaryTradeListByTime(routerContext.getUserCode(), routerContext.getProductCode(), OrderStatusEnum.CLOSING.getCode(), TradeTypeEnum.REAL_TRADE.getCode(), startDate, endDate);
        if (CollectionUtils.isEmpty(binaryTradeEntityList) || binaryTradeEntityList.size() < redisConfigUtil.getChangeOddsUserTradeNumbers()) {
            RuleResultVO ruleResultVO = new RuleResultVO(routerContext.getProductCode(), "新用户前10笔交易,收益率保持不变");
            ruleResultVO.setPriority(DEFAULT_PRIORITY);
            ruleResultVO.setMode(0);
            ruleResultVO.setChangeValue(BigDecimal.ZERO);
            ruleResultVO.setMessage("新用户前10笔交易,收益率保持不变");
            ruleResultVOList.add(ruleResultVO);
            return;
        }

        double todayTotalAmount = 0;
        double todayTotalProfit = 0;
        int effectiveNumber = 0;

        for (BinaryTradeEntity binaryTradeEntity : binaryTradeEntityList) {
            if (binaryTradeEntity.getAmount().compareTo(binaryTradeEntity.getProfitAmount()) == 0) {
                continue;
            }
            todayTotalAmount += binaryTradeEntity.getAmount().doubleValue();
            todayTotalProfit += (binaryTradeEntity.getProfitAmount().doubleValue() - binaryTradeEntity.getAmount().doubleValue());
            effectiveNumber++;
        }

        if (todayTotalProfit >= (todayTotalAmount / effectiveNumber * 2)) {
            RuleResultVO ruleResultVO = new RuleResultVO(routerContext.getProductCode(), "净利润≥2倍件均下单金额时,收益率比大盘收益率低3%");
            ruleResultVO.setPriority(DEFAULT_PRIORITY);
            ruleResultVO.setMode(0);
            ruleResultVO.setChangeValue(BigDecimal.valueOf(-0.03));
            ruleResultVO.setMessage("净利润≥2倍件均下单金额时,收益率比大盘收益率低3%");
            ruleResultVOList.add(ruleResultVO);
        } else {
            ruleResultVOList.add(new RuleResultVO(routerContext.getProductCode(), "净利润≥2倍件均下单金额时,收益率比大盘收益率低3%").setMessage("净利润≥2倍件均下单金额时,收益率比大盘收益率低3%-条件不满足"));
        }
    }
}
